/**
 * FQA内容列表。
 * @author linzongxue 2012-2-26
 */
define(function(){
	var PostGridPanel = Ext.extend(Ext.grid.GridPanel, {
		typePanel: null, //FQA类型树面板
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    autoExpandColumn: 4,
	    initComponent: function(){
	    	var expander = new Ext.ux.grid.RowExpander({
	            tpl : new Ext.Template(
	                '<div style="padding:4px;"><b>答案：</b><br/> {answer}</div>'
	            ),
	            listeners__: {
	            	beforeexpand: function(expander, record){
	            		record.set('answer2', record.get('answer').trim().replace(/\n/g, '<br/>'));
	            		record.commit();
	            	}
	            }
	        });
	    	PostGridPanel.superclass.initComponent.call(Ext.apply(this, {
	    		tbar: this.createTbar(),
	    		store: new Ext.data.JsonStore({
				    idProperty: 'id',
				    fields: ['typeName','question', 'answer'],
				    url: 'misc/fqa/getAllPostsOfType.do',
				    listeners:{
				    	load: function(store, records, options){
				    		Ext.each(records, function(r){
				    			r.set('answer', '　　' + r.get('answer').trim().replace(/\n/g, '<br/>'));
				    		});
				    	}
				    }
				}),
				columns: [
				    expander,
					new Ext.grid.RowNumberer(),
				    this.sm = new Ext.grid.CheckboxSelectionModel(),
					{header: '问题', width: 350, dataIndex: 'question'},
					//{header: '答案', width: 80, dataIndex: 'answer'},
					{header: '类型', width: 80, dataIndex: 'typeName'}
				],
				plugins: expander
	    	}));
	    },
	    refresh: function(){
	    	this.store.load({params:{typeId: this.fqaType.id}});
	    },
	    addPost: function(){
	    	use('misc.fqa.PostWindow', function(Win){
	    		new Win({
	    			fqaType: this.fqaType,
	    			callback: function(data, typePath){
	    				this.typePanel.selectType(typePath);
	    			},
	    			cbScope: this
	    		}).show();
	    	}, this);
	    },
	    modifyPost: function(){
			var record = this.getSelectionModel().getSelected();
			if (!record){
				cu.hint('请选择要修改的FQA');
				return;
			}
	    	use('misc.fqa.PostWindow', function(Win){
	    		new Win({
	    			fqaType: this.fqaType,
	    			modifiedId: record.id,
	    			callback: function(data, typePath){
	    				this.typePanel.selectType(typePath);
	    			},
	    			cbScope: this
	    		}).show();
	    	}, this);
	    },
	    removePost: function(){
	    	cu.removeInGrid({
	    		grid: this,
	    		recordName: 'FQA',
	    		handler: function(records){
	    			cu.post({
	    				url: 'misc/fqa/deletePosts.do',
	    				params: {ids: lu.pluck(records, 'id')},
	    				success: function(){
							this.getStore().remove(records);
	    				},
	    				scope: this
	    			});
	    		}
	    	});
	    },
	    setFqaType: function(fqaType){
	    	this.fqaType = fqaType;
	    },
	    createTbar: function(){
	    	return au.permit([
	    		{xtype:'label', text:'FQA列表', style:'font-weight:bold;margin-right:8px;'},
            	{text: '添加', permitId:'misc.fqa.addPost', handler: this.addPost, iconCls: 'icon-add', scope: this, tooltip:'添加FQA'},
            	{text: '修改', permitId:'misc.fqa.modifyPost', handler: this.modifyPost, iconCls: 'icon-edit',scope: this, tooltip:'修改FQA'},
            	{text: '删除', permitId:'misc.fqa.deletePost', handler: this.removePost, iconCls: 'icon-delete',scope: this, tooltip:'删除FQA'},
				'-',
				{text: '刷新', handler: this.refresh, iconCls: 'icon-refresh', scope: this}
            ]);
	    }
	});
	return PostGridPanel;
});